package com.leon.learnenginespace.drools.utils;

import com.leon.learnenginespace.drools.dao.BindMapper;
import com.leon.learnenginespace.drools.dao.DictionaryMapper;
import com.leon.learnenginespace.drools.dao.RuleMapper;
import com.leon.learnenginespace.drools.pojo.TDrlNSceneRuleBindConfig;
import com.leon.learnenginespace.drools.pojo.TDrlRuleConfi;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class DealRuleUtil {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    RuleMapper ruleMapper;
    @Autowired
    DictionaryMapper dictionaryMapper;
    @Autowired
    BindMapper bindMapper;

    /**
     * 保存规则表
     * @param ruleConfig
     * @throws Exception
     */
    public void updateRule (TDrlRuleConfi ruleConfig) throws Exception {

        int updateRow = 0;
        if(StringUtils.isEmpty(ruleConfig.getId())) {
//            updateRow = updateRuleBL.ruleConfigCreat(ruleConfig);
        } else {
            updateRow = ruleMapper.tDrlRuleConfigUpdate(ruleConfig);
        }
        if (updateRow <= 0) {
            logger.error("规则保存失败");
            throw new ParamException("保存失败");
        }
        logger.info("规则保存成功");
    }

    /**
     * 绑定场景
     * @param sceneRuleBindConfig
     */
    public void bindScene (TDrlNSceneRuleBindConfig sceneRuleBindConfig) {

        // 插入数据在场景规则表
        int insertBindRow = bindMapper.tDrlSceneConfiginsert(sceneRuleBindConfig);
        // 更新规则表的绑定场景ID
        int updateBindRow = bindMapper.updateBinds(sceneRuleBindConfig.getSceneConfigId(), sceneRuleBindConfig.getRuleConfigId());
        if (insertBindRow <=0 || updateBindRow <= 0) {
            logger.error("规则绑定失败");
            throw new ParamException("保存失败");
        }
        logger.info("规则绑定成功");
    }

    /**
     * 更新场景规则绑定表
     * @param sceneRuleBindConfig
     */
    public void updateBind (TDrlNSceneRuleBindConfig sceneRuleBindConfig) {
        int updateBindRow = bindMapper.tDrlSceneConfigNutieUpdate(sceneRuleBindConfig);// 更新场景规则绑定表的规则内容
        if (updateBindRow <= 0) {
            logger.error("场景规则绑定表保存失败");
            throw new ParamException("保存失败");
        }
    }

    /**
     * 修改规则状态为上架或下架
     * @param sceneRuleBindConfig
     */
    public void updateRuleStatus (TDrlNSceneRuleBindConfig sceneRuleBindConfig) {

        // 修改规则场景绑定表及规则表状态
        int updateRowNum = dictionaryMapper.updateStatus(sceneRuleBindConfig);
        int updateRuleRowNum = dictionaryMapper.updateRuleStatus(sceneRuleBindConfig);
        int updateVersionRowNum = dictionaryMapper.updateNVersion(sceneRuleBindConfig.getSceneCode());
        if (updateRowNum == 0 || updateRuleRowNum == 0 || updateVersionRowNum == 0) {
            logger.error("规则状态修改失败");
            throw new ParamException("保存失败");
        }
    }

}
